package server;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.IOException;

import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;

/**
 * This class overrides some of the methods in the abstract 
 * superclass in order to give more functionality to the server.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer extends AbstractServer 
{
  
	/** makes an instance of database class */
	public static  DataBase data=new DataBase();
	/** set default port number */
   final public static int DEFAULT_PORT = 5555;
  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the echo server.
   *
   * @param port The port number to connect on.
   */
  public EchoServer(int port) 
  {
    super(port);
  }

  
  //Instance methods ************************************************
  
  /**
   * This method handles any messages received from the client.
   *
   * @param msg The message received from the client.
   * @param client The connection from which the message originated.
   */
  public void handleMessageFromClient(Object msg, ConnectionToClient client)
  {
	  Object result=null;
	  String st[];
	  st=(String[])msg;
	  try 
	  {
	  if(st[1].equals("login"))  
		  		result=data.Login(st);
	  else if(st[1].equals("logout")) 
		  		result=data.Logout(st);	
	  else if (st[1].equals("searchbook"))
	      	  	result=data.ySearch(st);
	  else if(st[1].equals("retrieve_cart"))
		  		result=data.retrieve_cart(st);
	  else if(st[1].equals("checkMembership_BookBuy") || st[1].equals("checkMembership_MembershipBuy"))
	      		result=data.checkMembership(st);
	  else if(st[1].equals("purchase"))
	    		result=data.gPurchase(st);
	  else if(st[1].equals("gremove"))
	  			result=data.gRemove(st);
	  else if(st[1].equals("update Membership"))
	  			result=data.gupdate(st);
	  else if(st[1].equals("retrieve_book"))
	  			result=data.gbooks(st);
	  else if(st[1].equals("gAddReview"))
	  			result=data.gAddReview(st);
	  else if(st[1].equals("addtocart"))
	  			result=data.gAddToCart(st);
	  else if(st[1].equals("getFile"))
	  			result=data.getFile(st);
	  else if(st[1].equals("getpurchase"))
	  			result=data.gGetPurchase(st);
	else if(st[1].equals("YgetFieldsSubject"))
			result=data.getField(st);
  	else if(st[1].equals("getSubjects2"))
			result=data.getSubjects(st);
	else if(st[1].equals("YgetFields"))
			result=data.getField(st);
	else if (st[1].equals("searchbook"))
			result=data.ySearch(st);
	else if(st[1].equals("retrieve_cart"))
			result=data.retrieve_cart(st);
	else if(st[1].equals("retrieve_details") || st[1].equals("retrieve_language"))
			result=data.retrieve_details(st);
	else if(st[1].equals("update_details"))
		    result=data.upDateDetails(st);
	else if(st[1].equals("add_new_book"))
			result=data.add_new_book(st);
	 else if(st[1].equals("remove_from_catalog"))
		 	result=data.remove_from_catalog(st);
	 else if(st[1].equals("check"))
	  		result=data.check(st);
	 else if(st[1].equals("searchreview"))
			result=data.SearchR(st);
	 else if(st[1].equals("getreview"))
			result=data.greview(st);
	 else if(st[1].equals("retriveDB"))
			result=data.getDB(st);
	 else if(st[1].equals("restoreB"))
			result=data.resBook(st);
     else if(st[1].equals("searchuseru"))
        	result=data.lSearchUseru(st);
	 else if(st[1].equals("searchuserug")) 
        	result=data.gSearchUseru(st); 
     else if(st[1].equals("updatef"))
   	    	result=data.lUpdateF(st);
     else if(st[1].equals("searchuserreport"))
  	     	result=data.lSearchUserReaderReport(st);
     else if(st[1].equals("searchbookp"))
	     	result=data.lSearchBookp(st);
     else if(st[1].equals("searchbooks"))
    	 	result=data.lSearchBooks(st);
     else if(st[1].equals("searchupdate"))
      		result=data.lSearchUpdate(st);
   	 else if(st[1].equals("getFields") || st[1].equals("getFieldsSubject") || st[1].equals("getFields2")|| st[1].equals("getFieldsSubject1"))
	 	    result=data.getField(st);
	 else if(st[1].equals("getSubjects")|| st[1].equals("getSubjects1"))
	       	result=data.getSubjects(st);
	 else if(st[1].equals("addSubject"))
	   		result=data.addSubject(st);
	 else if(st[1].equals("RemoveSubject"))
	 		result=data.removeSubject(st);
	 else if(st[1].equals("addField"))
	 		result=data.addField(st);
	 else if(st[1].equals("RemoveField"))
			result=data.removeField(st);
	 else if(st[1].equals("getUnpostedReviews"))
		 	result=data.getReviews(st);
	 else if(st[1].equals("ReadUnpostedReview"))
			result=data.ReadUnpostedReview(st);
	 else if(st[1].equals("ConfirmReview")|| st[1].equals("RemoveReview"))
		 	result=data.ConfirmReview(st);
	 else if(st[1].equals("getReviewCount"))
			result=data.unpostedReviewCount(st);
	 else if(st[1].equals("GetBooksSubjects"))
			result=data.GetBooksSubjects(st);
	 else if(st[1].equals("AddSubjectForBook"))
			result=data.AddSubjectForBook(st);
	 else if(st[1].equals("RemoveSubjectForBook"))
			result=data.RemoveSubjectForBook(st);							      	 	
		client.sendToClient(result);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  /**
   * This method overrides the one in the superclass.  Called
   * when the server starts listening for connections.
   */
  protected void serverStarted()
  {
    System.out.println
      ("Server listening for connections on port " + getPort());
  }
  
  /**
   * This method overrides the one in the superclass.  Called
   * when the server stops listening for connections.
   */
  protected void serverStopped()
  {
    System.out.println
      ("Server has stopped listening for connections.");
  }
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of 
   * the server instance (there is no UI in this phase).
   *
   * @param args[0] The port number to listen on.  Defaults to 5555 
   *          if no argument is entered.
   */
  public static void main(String[] args) 
  {
    int port = 0; //Port to listen on

    try
    {
      port = Integer.parseInt(args[0]); //Get port from command line
    }
    catch(Throwable t)
    {
      port = DEFAULT_PORT; //Set port to 5555
    }
	
    EchoServer sv = new EchoServer(port);
    
    try 
    {
      sv.listen(); //Start listening for connections
    } 
    catch (Exception ex) 
    {
      System.out.println("ERROR - Could not listen for clients!");
    }
  }
}
//End of EchoServer class
  